11. Decorators

Decorators

Question:

Start Quiz:

# decorated.py
#
# Try running this code as it is.
# Then decorate the sumUp function by uncommenting the decorator call on the
# line above it, and run it again.
#
# To see the source for the decorator, look in the "timer.py" tab.

import timer

# Decorate me!  Uncomment the next line.
# @timer.timedFunction
def sumUp(n):
    total = 0
    for x in range(n):
        total += x
    return total

print sumUp(10000000)
# timer.py

import time

# Decorator function that prints the time that a function takes to run.
def timedFunction(func):
    # The wrapper function will take whatever arguments the original does.
    def wrapper(*args, **kwargs):
        # Call the function we're decorating, recording its start and end time.
        start = time.time()
        value = func(*args, **kwargs)
        end = time.time()

        # Calculate and print the elapsed time of the decorated function.
        elapsed = end - start
        print "%s took %2.4f seconds to run." % (func.__name__, elapsed)

        # Return the decorated function's value.
        return value
    return wrapper
Solution: